import { _decorator, Component, Node, instantiate, Prefab, view, director, v2, tween, v3, Sprite, Color, UIOpacity } from 'cc';
import { Plant } from './Plant';
import { Util } from './Util';

const { ccclass, property } = _decorator;
import { GameData, GameState } from './GameData';
const gameData = GameData.getInstance()


@ccclass('Sunflower')
export class Sunflower extends Plant {
    @property(Prefab)
    sunEnergyPrefab = null


    start() {
        this.schedule(this.createSunEnergy, 5)
    }

    update(deltaTime: number) {

    }
    startAnimation(target) {
        // 定义花朵的目标位置
        let endPos = [v3(-100, 20, 0), v3(100, -20, 0), v3(10, 100, 0)][~~(Math.random() * 3)]; // 目标位置，根据需要调整
        const targetAlpha = target.getComponent(UIOpacity)
        targetAlpha.opacity = 0;
        tween(targetAlpha)
            .to(0.5, { opacity: 255 })
            .start();
        
        target.setScale(v3(0, 0, 0));
            
        // 使用tween创建动画
        tween(target)
            .to(0.5, { position: endPos }, { easing: 'cubicOut' })
            .start();

        tween(target)
            .to(0.3, { scale: v3(0.75, 0.75, 0.75) }, { easing: 'elasticIn' }) // 缩放回原大小
            .start();
    }
    createSunEnergy() {
        if(gameData.gameSate !== GameState.ready) return;

        let sun = instantiate(this.sunEnergyPrefab);
        this.node.addChild(sun);
        this.startAnimation(sun)
    }
}


